<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">







  <link rel="alternate" href="/atom.xml" title="最好的安排" type="application/atom+xml" />






<meta name="description" content="静态PDO操作类使用说明">
<meta name="keywords" content="pdo封装,mysql操作,CURD">
<meta property="og:type" content="article">
<meta property="og:title" content="静态PDO基础操作封装">
<meta property="og:url" content="https://blog.wm07.cn/2017/pdo_func.html">
<meta property="og:site_name" content="最好的安排">
<meta property="og:description" content="静态PDO操作类使用说明">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-12-16T01:47:32.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="静态PDO基础操作封装">
<meta name="twitter:description" content="静态PDO操作类使用说明">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://blog.wm07.cn/2017/pdo_func.html"/>





  <title>静态PDO基础操作封装 | 最好的安排</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">最好的安排</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">深度</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.wm07.cn/2017/pdo_func.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="空城">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://cdn.wm07.cn/pic.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="最好的安排">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">静态PDO基础操作封装</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-14T16:48:37+08:00">
                2017-12-14
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/pdo_func.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2017/pdo_func.html" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2017/pdo_func.html" class="leancloud_visitors" data-flag-title="静态PDO基础操作封装">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          
              <div class="post-description">
                  静态PDO操作类使用说明
              </div>
          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>环境要求:PHP7.1 PDO Extension</p>
</blockquote>
<h3 id="先贴代码"><a href="#先贴代码" class="headerlink" title="先贴代码"></a>先贴代码</h3><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br><span class="line">510</span><br><span class="line">511</span><br><span class="line">512</span><br><span class="line">513</span><br><span class="line">514</span><br><span class="line">515</span><br><span class="line">516</span><br><span class="line">517</span><br><span class="line">518</span><br><span class="line">519</span><br><span class="line">520</span><br><span class="line">521</span><br><span class="line">522</span><br><span class="line">523</span><br><span class="line">524</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * PDO FOR MySQL Extension</span></span><br><span class="line"><span class="comment"> * Author 空城 &lt;694623056<span class="doctag">@qq</span>.com&gt;</span></span><br><span class="line"><span class="comment"> * Copyright 2017 空城</span></span><br><span class="line"><span class="comment"> * CreateDate 2017-12-14</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">mysql_pdo</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">// Host address</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $host     = <span class="string">'127.0.0.1'</span>;</span><br><span class="line">    <span class="comment">// Host port</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $port     = <span class="number">3306</span>;</span><br><span class="line">    <span class="comment">// Username</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $user     = <span class="string">'root'</span>;</span><br><span class="line">    <span class="comment">// Password</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $pwd      = <span class="string">'root'</span>;</span><br><span class="line">    <span class="comment">// Database</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $db       = <span class="string">''</span>;</span><br><span class="line">    <span class="comment">// Character set</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $charset  = <span class="string">'utf8mb4'</span>;</span><br><span class="line">    <span class="comment">// Persistent connection</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $pconnect = <span class="keyword">true</span>;</span><br><span class="line">    <span class="comment">// Connection object</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $conn     = <span class="keyword">null</span>;</span><br><span class="line">    <span class="comment">// Table name</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $table    = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Core container</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $data     = <span class="string">''</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $field    = <span class="string">'*'</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $where    = <span class="string">''</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $order    = <span class="string">''</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $group    = <span class="string">''</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $limit    = <span class="string">''</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $join     = <span class="string">''</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $bind     = [];</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $sql      = <span class="string">''</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Initialization</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">init</span><span class="params">(array $conf = array<span class="params">()</span>, bool $reconnect = false)</span>:<span class="title">void</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        class_exists(<span class="string">'PDO'</span>) <span class="keyword">or</span> <span class="keyword">exit</span>(<span class="string">"PDO: class not exists."</span>);</span><br><span class="line">        <span class="keyword">empty</span>($conf[<span class="string">'host'</span>])  <span class="keyword">or</span> <span class="keyword">self</span>::$host  = $conf[<span class="string">'host'</span>];</span><br><span class="line">        <span class="keyword">empty</span>($conf[<span class="string">'port'</span>])  <span class="keyword">or</span> <span class="keyword">self</span>::$port  = $conf[<span class="string">'port'</span>];</span><br><span class="line">        <span class="keyword">empty</span>($conf[<span class="string">'user'</span>])  <span class="keyword">or</span> <span class="keyword">self</span>::$user  = $conf[<span class="string">'user'</span>];</span><br><span class="line">        <span class="keyword">empty</span>($conf[<span class="string">'pwd'</span>])   <span class="keyword">or</span> <span class="keyword">self</span>::$pwd   = $conf[<span class="string">'pwd'</span>];</span><br><span class="line">        <span class="keyword">empty</span>($conf[<span class="string">'db'</span>])    <span class="keyword">or</span> <span class="keyword">self</span>::$db    = $conf[<span class="string">'db'</span>];</span><br><span class="line">        <span class="keyword">empty</span>($conf[<span class="string">'table'</span>]) <span class="keyword">or</span> <span class="keyword">self</span>::$table = $conf[<span class="string">'table'</span>];</span><br><span class="line">        <span class="keyword">if</span> (is_null(<span class="keyword">self</span>::$conn) || $reconnect) <span class="keyword">self</span>::$conn = <span class="keyword">self</span>::_connect();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Query or Exec</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span>  <span class="function"><span class="keyword">function</span> <span class="title">do</span><span class="params">(string $sql = <span class="string">''</span>, bool $flag = false)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">empty</span>($sql) <span class="keyword">or</span> <span class="keyword">self</span>::$sql = $sql;</span><br><span class="line">        $preg = <span class="string">'INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|SELECT .* INTO|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK'</span>; </span><br><span class="line">        <span class="keyword">if</span> (preg_match(<span class="string">'/^\s*"?('</span> . $preg . <span class="string">')\s+/i'</span>, <span class="keyword">self</span>::$sql)) <span class="keyword">return</span> <span class="keyword">self</span>::exec(<span class="string">''</span>, $flag);</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">return</span> <span class="keyword">self</span>::query(<span class="string">''</span>, <span class="keyword">self</span>::$sql);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Query</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">query</span><span class="params">(string $sql = <span class="string">''</span>, bool $flag = false)</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $statm = <span class="keyword">self</span>::_start($sql);</span><br><span class="line">        $result = $statm-&gt;fetchAll(\PDO::FETCH_ASSOC);</span><br><span class="line">        <span class="keyword">return</span> $flag ? $result[<span class="number">0</span>] : $result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Exec</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">exec</span><span class="params">(string $sql = <span class="string">''</span>, bool $flag = false)</span>:<span class="title">int</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $statm = <span class="keyword">self</span>::_start($sql);</span><br><span class="line">        $row = $statm-&gt;rowCount();</span><br><span class="line">        <span class="keyword">return</span> $flag ? <span class="keyword">self</span>::$conn-&gt;lastInsertId() : $row;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Insert </span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">insert</span><span class="params">(string $table = <span class="string">''</span>, array $data = [], bool $flag = false)</span>:<span class="title">int</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"></span><br><span class="line">        $table = !<span class="keyword">empty</span>($data) ? $table : <span class="keyword">self</span>::$table;</span><br><span class="line">        $data = !<span class="keyword">empty</span>($data) ? $data : <span class="keyword">self</span>::$data;</span><br><span class="line">        $insertData = [];</span><br><span class="line">        <span class="keyword">if</span> ( count($data) == count($data, <span class="number">1</span>) ) $insertData[<span class="number">0</span>] = $data;</span><br><span class="line">        <span class="keyword">else</span> $insertData = $data;</span><br><span class="line">        $lastId = <span class="number">0</span>;</span><br><span class="line">        $row = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">foreach</span> ($insertData <span class="keyword">as</span> $key =&gt; $data) &#123;</span><br><span class="line">            $data = <span class="keyword">self</span>::_format($table, $data);</span><br><span class="line">            $vals = [];</span><br><span class="line">            <span class="keyword">foreach</span> ($data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">                $vals[] = <span class="keyword">self</span>::_setBind(str_replace(<span class="string">'`'</span>, <span class="string">''</span>, $key), $value);</span><br><span class="line">            &#125;</span><br><span class="line">            $keys = array_keys($data);</span><br><span class="line">            </span><br><span class="line">            <span class="keyword">self</span>::$sql = <span class="string">'INSERT INTO `'</span> . trim($table) . <span class="string">'` ('</span> . implode(<span class="string">','</span>, $keys) . <span class="string">') VALUES('</span> . implode(<span class="string">','</span>, $vals) . <span class="string">')'</span>;</span><br><span class="line">            <span class="keyword">self</span>::exec() &amp;&amp; $flag &amp;&amp; $row += <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        $lastId = <span class="keyword">self</span>::$conn-&gt;lastInsertId();</span><br><span class="line">        <span class="keyword">unset</span>($insertData,$data);</span><br><span class="line">        <span class="keyword">return</span> $flag ? $row : $lastId;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Delete</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">del</span><span class="params">(string $table = <span class="string">''</span>, array $where = [])</span>:<span class="title">int</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $table = !<span class="keyword">empty</span>($data) ? $table : <span class="keyword">self</span>::$table;</span><br><span class="line">        $where = !<span class="keyword">empty</span>($where) ? <span class="keyword">self</span>::_where($where) : <span class="keyword">self</span>::_where(<span class="keyword">self</span>::$where);</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">''</span> === $where) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">self</span>::$sql = <span class="string">'DELETE FROM `'</span>.trim($table).<span class="string">'` '</span>.$where;</span><br><span class="line">        <span class="keyword">unset</span>($table, $where);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::exec();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Update</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">save</span><span class="params">(string $table = <span class="string">''</span>, array $data = [], $where = [])</span>:<span class="title">int</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $table = !<span class="keyword">empty</span>($table) ? $table : <span class="keyword">self</span>::$table;</span><br><span class="line">        $data = !<span class="keyword">empty</span>($data) ? $data : <span class="keyword">self</span>::$data;</span><br><span class="line">        $where = !<span class="keyword">empty</span>($where) ? $where : <span class="keyword">self</span>::$where;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">false</span> == $where) &#123;</span><br><span class="line">            $key = <span class="keyword">self</span>::_tbKey($table);</span><br><span class="line">            $where = [];</span><br><span class="line">            <span class="keyword">foreach</span> ($key <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">                <span class="keyword">empty</span>($data[$k]) <span class="keyword">or</span> $where[$k] = $data[$k];</span><br><span class="line">            &#125;</span><br><span class="line">            $where = <span class="keyword">self</span>::_where($where);</span><br><span class="line">        &#125; <span class="keyword">else</span> $where = <span class="keyword">self</span>::_where($where);</span><br><span class="line">        $data = <span class="keyword">self</span>::_format($table, $data);</span><br><span class="line">        $kv = [];</span><br><span class="line">        <span class="keyword">foreach</span> ($data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            $k = str_replace(<span class="string">'`'</span>, <span class="string">''</span>, $key);</span><br><span class="line">            $k = <span class="keyword">self</span>::_setBind($k, $value);</span><br><span class="line">            $kv[] = $key.<span class="string">'='</span>.$k;</span><br><span class="line">        &#125;</span><br><span class="line">        $kv_str = implode(<span class="string">','</span>, $kv);</span><br><span class="line">        <span class="keyword">self</span>::$sql = <span class="string">'UPDATE `'</span>.trim($table).<span class="string">'` SET '</span>.trim($kv_str).<span class="string">' '</span>.trim($where);</span><br><span class="line">        <span class="keyword">unset</span>($kv_str, $data, $kv, $table);</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">''</span> === $where) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::exec();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Select</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">select</span><span class="params">(string $table = <span class="string">''</span>, array $opt = [])</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $opt = <span class="keyword">self</span>::_condition($table, $opt);</span><br><span class="line">        $field = $opt[<span class="string">'field'</span>] = !<span class="keyword">empty</span>($opt[<span class="string">'field'</span>]) ? $opt[<span class="string">'field'</span>] : <span class="keyword">self</span>::$field;</span><br><span class="line">        <span class="keyword">if</span> (is_array($field)) &#123;</span><br><span class="line">            <span class="keyword">foreach</span> ($field <span class="keyword">as</span> $key =&gt; $value) $field[$key] = <span class="keyword">self</span>::_avoidKey($value);</span><br><span class="line">            $field = implode(<span class="string">','</span>, $field);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">elseif</span>(is_string($field) &amp;&amp; $field != <span class="string">''</span>);</span><br><span class="line">        <span class="keyword">else</span> $field = <span class="string">'*'</span>;</span><br><span class="line">        <span class="keyword">self</span>::$sql = <span class="string">'SELECT '</span>.$field.<span class="string">' FROM `'</span>.$opt[<span class="string">'table'</span>].<span class="string">'` '</span>.$opt[<span class="string">'join'</span>].$opt[<span class="string">'where'</span>].$opt[<span class="string">'group'</span>].$opt[<span class="string">'order'</span>].$opt[<span class="string">'limit'</span>];</span><br><span class="line">        <span class="keyword">unset</span>($opt);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::query();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Get a line</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">first</span><span class="params">(string $table = <span class="string">''</span>, array $opt = [])</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">self</span>::$limit = <span class="string">'1'</span>;</span><br><span class="line">        $result = <span class="keyword">self</span>::select($table, $opt);</span><br><span class="line">        <span class="keyword">return</span> $result[<span class="number">0</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Count</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">count</span><span class="params">(string $table = <span class="string">''</span>, array $opt = [])</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $option = <span class="keyword">self</span>::_condition($table,$opt);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::_common($option, <span class="string">'count'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Avg</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">avg</span><span class="params">(string $table = <span class="string">''</span>, array $opt = [])</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $option = <span class="keyword">self</span>::_condition($table,$opt);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::_common($option, <span class="string">'avg'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Sum</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">sum</span><span class="params">(string $table = <span class="string">''</span>, array $opt = [])</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $option = <span class="keyword">self</span>::_condition($table,$opt);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::_common($option, <span class="string">'sum'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Min</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">min</span><span class="params">(string $table = <span class="string">''</span>, array $opt = [])</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $option = <span class="keyword">self</span>::_condition($table,$opt);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::_common($option, <span class="string">'min'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Max</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">max</span><span class="params">(string $table = <span class="string">''</span>, array $opt = [])</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $option = <span class="keyword">self</span>::_condition($table,$opt);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::_common($option, <span class="string">'max'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Dec</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">dec</span><span class="params">(string $table = <span class="string">''</span>, $data = [], $where = [])</span>:<span class="title">int</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::_setCol($table, $data, $where,<span class="string">'-'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Inc</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">inc</span><span class="params">(string $table = <span class="string">''</span>, $data = [], $where = [])</span>:<span class="title">int</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::_setCol($table, $data, $where,<span class="string">'+'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Clear</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">clear</span><span class="params">()</span>:<span class="title">void</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">self</span>::$data  = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">self</span>::$field = <span class="string">'*'</span>;</span><br><span class="line">        <span class="keyword">self</span>::$where = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">self</span>::$order = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">self</span>::$group = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">self</span>::$limit = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">self</span>::$bind  = [];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// SetAttribute</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">setAttr</span><span class="params">($key, $val)</span>:<span class="title">bool</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        !<span class="keyword">empty</span>(<span class="keyword">self</span>::$conn) <span class="keyword">or</span> <span class="keyword">self</span>::_connect();</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::$conn-&gt;setAttribute($key, $val);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// BeginTransaction</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">begin</span><span class="params">()</span>:<span class="title">bool</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        !<span class="keyword">empty</span>(<span class="keyword">self</span>::$conn) <span class="keyword">or</span> <span class="keyword">self</span>::_connect();</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::$conn-&gt;beginTransaction();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Commit</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">commit</span><span class="params">()</span>:<span class="title">bool</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        !<span class="keyword">empty</span>(<span class="keyword">self</span>::$conn) <span class="keyword">or</span> <span class="keyword">self</span>::_connect();</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::$conn-&gt;commit();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// RollBack</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">rollBack</span><span class="params">()</span>:<span class="title">bool</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        !<span class="keyword">empty</span>(<span class="keyword">self</span>::$conn) <span class="keyword">or</span> <span class="keyword">self</span>::_connect();</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::$conn-&gt;rollBack();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Connect</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_connect</span><span class="params">()</span>:<span class="title">void</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $dsn = <span class="string">'mysql:host='</span>.<span class="keyword">self</span>::$host.<span class="string">';port='</span>.<span class="keyword">self</span>::$port.<span class="string">';dbname='</span>.<span class="keyword">self</span>::$db;</span><br><span class="line">        $options = [</span><br><span class="line">                \PDO::MYSQL_ATTR_INIT_COMMAND =&gt; <span class="string">'SET NAMES '</span> . <span class="keyword">self</span>::$charset,</span><br><span class="line">                \PDO::ATTR_PERSISTENT         =&gt; (bool)<span class="keyword">self</span>::$pconnect</span><br><span class="line">            ];</span><br><span class="line">        <span class="keyword">try</span> &#123; </span><br><span class="line">            $dbh = <span class="keyword">new</span> \PDO($dsn, <span class="keyword">self</span>::$user, <span class="keyword">self</span>::$pwd, $options);</span><br><span class="line">            $dbh-&gt;setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);</span><br><span class="line">            $dbh-&gt;setAttribute(\PDO::ATTR_EMULATE_PREPARES, <span class="keyword">false</span>);</span><br><span class="line">            $dbh-&gt;exec(<span class="string">'SET NAMES '</span> . <span class="keyword">self</span>::$charset);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (PDOException $e) &#123; </span><br><span class="line">            <span class="keyword">exit</span>(<span class="string">'Connection failed: '</span> . $e-&gt;getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">self</span>::$conn = $dbh;</span><br><span class="line">        <span class="keyword">unset</span>($dsn, $dbh, $options);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Mosaic SQL</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_condition</span><span class="params">(string $table, array $opt)</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $option = [];</span><br><span class="line">        $option[<span class="string">'table'</span>] = !<span class="keyword">empty</span>($table) ? $table : <span class="keyword">self</span>::$table;</span><br><span class="line">        $option[<span class="string">'field'</span>] = !<span class="keyword">empty</span>($opt[<span class="string">'field'</span>]) ? $opt[<span class="string">'field'</span>] : <span class="keyword">self</span>::$field;</span><br><span class="line">        $option[<span class="string">'join'</span>] = !<span class="keyword">empty</span>($opt[<span class="string">'join'</span>]) ? <span class="keyword">self</span>::_join($opt[<span class="string">'join'</span>]) :<span class="keyword">self</span>::_join(<span class="keyword">self</span>::$join);</span><br><span class="line">        $option[<span class="string">'where'</span>] = !<span class="keyword">empty</span>($opt[<span class="string">'where'</span>]) ? <span class="keyword">self</span>::_where($opt[<span class="string">'where'</span>]) : <span class="keyword">self</span>::_where(<span class="keyword">self</span>::$where);</span><br><span class="line">        $option[<span class="string">'order'</span>] = !<span class="keyword">empty</span>($opt[<span class="string">'order'</span>]) ? <span class="keyword">self</span>::_order($opt[<span class="string">'order'</span>]) : <span class="keyword">self</span>::_order(<span class="keyword">self</span>::$order);</span><br><span class="line">        $option[<span class="string">'group'</span>] = !<span class="keyword">empty</span>($opt[<span class="string">'group'</span>]) ? <span class="keyword">self</span>::_group($opt[<span class="string">'group'</span>]) : <span class="keyword">self</span>::_group(<span class="keyword">self</span>::$group);</span><br><span class="line">        $option[<span class="string">'limit'</span>] = !<span class="keyword">empty</span>($opt[<span class="string">'limit'</span>]) ? <span class="keyword">self</span>::_limit($opt[<span class="string">'limit'</span>]) : <span class="keyword">self</span>::_limit(<span class="keyword">self</span>::$limit);</span><br><span class="line">        <span class="keyword">return</span> $option;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Exec SQL common function</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_start</span><span class="params">(string $sql = <span class="string">''</span>)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">empty</span>($sql) <span class="keyword">or</span> <span class="keyword">self</span>::$sql = $sql;</span><br><span class="line">        !<span class="keyword">empty</span>(<span class="keyword">self</span>::$conn) <span class="keyword">or</span> <span class="keyword">self</span>::_connect();</span><br><span class="line">        $statm = <span class="keyword">self</span>::$conn-&gt;prepare(<span class="keyword">self</span>::$sql);</span><br><span class="line">        $statm-&gt;execute(<span class="keyword">self</span>::$bind);</span><br><span class="line">        <span class="keyword">self</span>::clear();</span><br><span class="line">        <span class="keyword">return</span> $statm;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Common</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_common</span><span class="params">(array $opt, string $func)</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (is_string($opt[<span class="string">'field'</span>]) &amp;&amp; $opt[<span class="string">'field'</span>] != <span class="string">""</span>) &#123;</span><br><span class="line">            $strField = $opt[<span class="string">'field'</span>];</span><br><span class="line">            $fieldArr = explode(<span class="string">","</span>, $strField);</span><br><span class="line">            $strField = <span class="string">'_'</span>.implode(<span class="string">"_,_"</span>, $fieldArr).<span class="string">'_'</span>;</span><br><span class="line">        &#125; <span class="keyword">elseif</span> (is_array($opt[<span class="string">'field'</span>])) &#123;</span><br><span class="line">            $fieldArr = $opt[<span class="string">'field'</span>];</span><br><span class="line">            $strField = <span class="string">'_'</span>.implode(<span class="string">"_,_"</span>, $opt[<span class="string">'field'</span>]).<span class="string">'_'</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">foreach</span> ($fieldArr <span class="keyword">as</span> $v) &#123;</span><br><span class="line">            $val = <span class="keyword">self</span>::_avoidKey($v);</span><br><span class="line">            $alias = str_replace(<span class="string">'.'</span>, <span class="string">'_'</span>, $val);</span><br><span class="line">            $alias = <span class="string">' AS '</span>.(<span class="keyword">false</span> === strpos($val, <span class="string">'*'</span>) ? $alias : <span class="string">'`'</span>. $alias .<span class="string">'`'</span>);</span><br><span class="line">            $strField = str_replace(<span class="string">'_'</span>.$v.<span class="string">'_'</span>, $func . <span class="string">'('</span> . $val . <span class="string">') '</span>.$alias, $strField);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">self</span>::$sql = <span class="string">'SELECT '</span>.$strField.<span class="string">' FROM `'</span>.$opt[<span class="string">'table'</span>].<span class="string">'` '</span>.$opt[<span class="string">'join'</span>].$opt[<span class="string">'where'</span>].$opt[<span class="string">'group'</span>].$opt[<span class="string">'order'</span>].$opt[<span class="string">'limit'</span>];</span><br><span class="line">        <span class="keyword">unset</span>($opt, $func, $fieldArr, $strField, $alias);</span><br><span class="line">        $result = <span class="keyword">self</span>::query();</span><br><span class="line">        <span class="keyword">return</span> count($result) == <span class="number">1</span> &amp;&amp; !<span class="keyword">empty</span>($result[<span class="number">0</span>]) ? $result[<span class="number">0</span>] : $result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Set field</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_setCol</span><span class="params">(string $table = <span class="string">''</span>, $data = <span class="string">''</span>, $where = [], string $type)</span>:<span class="title">int</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $table = !<span class="keyword">empty</span>($table) ? $table : <span class="keyword">self</span>::$table;</span><br><span class="line">        $data  = !<span class="keyword">empty</span>($data) ? $data : <span class="keyword">self</span>::$data;</span><br><span class="line">        $where = !<span class="keyword">empty</span>($where) ? <span class="keyword">self</span>::_where($where) : <span class="keyword">self</span>::_where(<span class="keyword">self</span>::$where);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (is_array($data)) &#123;</span><br><span class="line">            $new_data = [];</span><br><span class="line">            <span class="keyword">foreach</span> ($data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">                <span class="keyword">if</span> (is_string($key)) $new_data[$key] = $key.$type.abs($value);</span><br><span class="line">                <span class="keyword">else</span> $new_data[$value] = $value.$type.<span class="string">'1'</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">elseif</span> (is_string($data)) $new_data[$data] = $data.$type.<span class="string">'1'</span>;</span><br><span class="line">        $kv = [];</span><br><span class="line">        <span class="keyword">foreach</span> ($new_data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            $kv[] = <span class="keyword">self</span>::_avoidKey($key).<span class="string">'='</span>.$value;</span><br><span class="line">        &#125;</span><br><span class="line">        $kv_str = implode(<span class="string">','</span>, $kv);</span><br><span class="line">        <span class="keyword">self</span>::$sql = <span class="string">'UPDATE `'</span>.trim($table).<span class="string">'` SET '</span>.trim($kv_str).<span class="string">' '</span>.trim($where);</span><br><span class="line">        <span class="keyword">unset</span>($data);</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">''</span> === $where) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::exec();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Preprocessing</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_setBind</span><span class="params">(string $key, $value)</span>:<span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">empty</span>(<span class="keyword">self</span>::$bind[<span class="string">':'</span>.$key])) &#123;</span><br><span class="line">            $k = <span class="string">':'</span>.$key;</span><br><span class="line">            <span class="keyword">self</span>::$bind[$k] = $value;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $k = <span class="string">':'</span>.$key.<span class="string">'_'</span>.mt_rand(<span class="number">1</span>,<span class="number">9999</span>);</span><br><span class="line">            <span class="keyword">while</span> (!<span class="keyword">empty</span>(<span class="keyword">self</span>::$bind[<span class="string">':'</span>.$k])) &#123;</span><br><span class="line">                $k = <span class="string">':'</span>.$key.<span class="string">'_'</span>.mt_rand(<span class="number">1</span>,<span class="number">9999</span>);</span><br><span class="line">            &#125;  </span><br><span class="line">            <span class="keyword">self</span>::$bind[$k] = $value;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">unset</span>($key, $value);</span><br><span class="line">        <span class="keyword">return</span> $k;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Join</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_join</span><span class="params">($opt)</span>:<span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $join = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">if</span> (is_string($opt) &amp;&amp; <span class="string">''</span> !== trim($opt)) <span class="keyword">return</span> $opt;</span><br><span class="line">        <span class="keyword">elseif</span> (is_array($opt)) &#123;</span><br><span class="line">            <span class="keyword">foreach</span>($opt <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">                $mode = <span class="string">'INNER'</span>;</span><br><span class="line">                <span class="keyword">if</span> (is_array($value)) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (!<span class="keyword">empty</span>($value[<span class="number">2</span>]) &amp;&amp; <span class="number">0</span> === strcasecmp($value[<span class="number">2</span>], <span class="string">'LEFT'</span>)) $mode = <span class="string">'LEFT'</span>;</span><br><span class="line">                    <span class="keyword">elseif</span> (!<span class="keyword">empty</span>($value[<span class="number">2</span>]) &amp;&amp; <span class="number">0</span> === strcasecmp($value[<span class="number">2</span>], <span class="string">'RIGHT'</span>)) $mode = <span class="string">'RIGHT'</span>;</span><br><span class="line">                    $relative = !<span class="keyword">empty</span>($value[<span class="number">3</span>]) ? $value[<span class="number">3</span>] : <span class="string">'='</span>;</span><br><span class="line">                    $condition = <span class="string">' '</span>.$mode.<span class="string">' JOIN '</span>.$key.<span class="string">' ON '</span>.<span class="keyword">self</span>::_avoidKey($value[<span class="number">0</span>]).$relative.<span class="keyword">self</span>::_avoidKey($value[<span class="number">1</span>]).<span class="string">' '</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    $condition = <span class="string">' '</span>.$mode.<span class="string">' JOIN '</span>.$key.<span class="string">' ON '</span>.$value.<span class="string">' '</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                $join .= $condition;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">unset</span>($opt);</span><br><span class="line">        <span class="keyword">return</span> $join;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Where</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_where</span><span class="params">($opt)</span>:<span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $where = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">if</span> (is_string($opt) &amp;&amp; <span class="string">''</span> !== trim($opt)) <span class="keyword">return</span> <span class="string">' WHERE '</span>.$opt;</span><br><span class="line">        <span class="keyword">elseif</span> (is_array($opt)) &#123;</span><br><span class="line">            <span class="keyword">foreach</span>($opt <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">                $k = <span class="keyword">self</span>::_avoidKey($key);</span><br><span class="line">                <span class="keyword">if</span> (is_array($value)) &#123;</span><br><span class="line">                    $key = <span class="keyword">self</span>::_setBind($key,$value[<span class="number">0</span>]);</span><br><span class="line">                    $relative = !<span class="keyword">empty</span>($value[<span class="number">1</span>]) ? $value[<span class="number">1</span>] : <span class="string">'='</span>;</span><br><span class="line">                    $link    = !<span class="keyword">empty</span>($value[<span class="number">2</span>]) ? $value[<span class="number">2</span>] : <span class="string">'AND'</span>;</span><br><span class="line">                    $condition = <span class="string">' ('</span>.$k.<span class="string">' '</span>.$relative.<span class="string">' '</span>.$key.<span class="string">') '</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    $key = <span class="keyword">self</span>::_setBind($key,$value);</span><br><span class="line">                    $link = <span class="string">'AND'</span>;</span><br><span class="line">                    $condition = <span class="string">' ('</span>.$k.<span class="string">'='</span>.$key.<span class="string">') '</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                $where .= $where !== <span class="string">''</span> ? $link.$condition : <span class="string">' WHERE '</span>.$condition;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">unset</span>($opt);</span><br><span class="line">        <span class="keyword">return</span> $where;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Order</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_order</span><span class="params">($opt)</span>:<span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $order = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">if</span> (is_string($opt) &amp;&amp; <span class="string">''</span> !== trim($opt)) <span class="keyword">return</span> <span class="string">' ORDER BY '</span>._avoidKey($opt);</span><br><span class="line">        <span class="keyword">elseif</span> (is_array($opt)) &#123;</span><br><span class="line">            <span class="keyword">foreach</span>($opt <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">                $link = <span class="string">','</span>;</span><br><span class="line">                <span class="keyword">if</span> (is_string($key)) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (<span class="number">0</span> === strcasecmp($value, <span class="string">'DESC'</span>)) $condition = <span class="string">' '</span>.<span class="keyword">self</span>::_avoidKey($key).<span class="string">' DESC '</span>;</span><br><span class="line">                    <span class="keyword">else</span> $condition = <span class="string">' '</span>.<span class="keyword">self</span>::_avoidKey($key).<span class="string">' ASC '</span>;</span><br><span class="line">                &#125;  <span class="keyword">else</span> $condition = <span class="string">' '</span>.<span class="keyword">self</span>::_avoidKey($value).<span class="string">' ASC '</span>;</span><br><span class="line">                $order .= $order !== <span class="string">''</span> ? $link.addslashes($condition) : <span class="string">' ORDER BY '</span>.addslashes($condition);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">unset</span>($opt);</span><br><span class="line">        <span class="keyword">return</span> $order;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Limit</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_limit</span><span class="params">($opt)</span>:<span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $limit = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">if</span> (is_string($opt) &amp;&amp; <span class="string">''</span> !== trim($opt)) <span class="keyword">return</span> <span class="string">' LIMIT '</span>.$opt;</span><br><span class="line">        <span class="keyword">elseif</span> (is_array($opt) &amp;&amp; <span class="number">2</span> == count($opt)) $limit = <span class="string">' LIMIT '</span>.(int)$opt[<span class="number">0</span>].<span class="string">','</span>.(int)$opt[<span class="number">1</span>];</span><br><span class="line">        <span class="keyword">elseif</span> (is_array($opt) &amp;&amp; <span class="number">1</span> == count($opt)) $limit = <span class="string">' LIMIT '</span>.(int)$opt[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">unset</span>($opt);</span><br><span class="line">        <span class="keyword">return</span> $limit;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Group</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_group</span><span class="params">($opt)</span>:<span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $group = <span class="string">''</span>;</span><br><span class="line">        <span class="keyword">if</span> (is_string($opt) &amp;&amp; <span class="string">''</span> !== trim($opt)) <span class="keyword">return</span> <span class="string">' GROUP BY '</span>._avoidKey($opt);</span><br><span class="line">        <span class="keyword">elseif</span> (is_array($opt)) &#123;</span><br><span class="line">            <span class="keyword">foreach</span>($opt <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">                $link = <span class="string">','</span>;</span><br><span class="line">                $condition = <span class="string">' '</span>.<span class="keyword">self</span>::_avoidKey($value).<span class="string">' '</span>;</span><br><span class="line">                $group .= $group !== <span class="string">''</span> ? $link.addslashes($condition) : <span class="string">' GROUP BY '</span>.addslashes($condition);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">unset</span>($opt);</span><br><span class="line">        <span class="keyword">return</span> $group;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Format data</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_format</span><span class="params">(string $table, $data)</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!is_array($data)) <span class="keyword">return</span> <span class="keyword">array</span>();</span><br><span class="line"></span><br><span class="line">        $tbColumn = <span class="keyword">self</span>::_tbInfo($table);</span><br><span class="line">        $res = [];</span><br><span class="line">        <span class="keyword">foreach</span> ($data <span class="keyword">as</span> $key =&gt; $val) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!is_scalar($val)) <span class="keyword">continue</span>;</span><br><span class="line">            <span class="keyword">if</span> (!<span class="keyword">empty</span>($tbColumn[$key])) &#123;</span><br><span class="line">                $key = <span class="keyword">self</span>::_avoidKey($key);</span><br><span class="line">                <span class="keyword">if</span> (is_int($val)) $val = intval($val);</span><br><span class="line">                <span class="keyword">elseif</span> (is_float($val)) $val = floatval($val); </span><br><span class="line">                <span class="keyword">elseif</span> (preg_match(<span class="string">'/^\(\w*(\+|\-|\*|\/)?\w*\)$/i'</span>, $val)) $val = $val;</span><br><span class="line">                <span class="keyword">elseif</span> (is_string($val)) $val = addslashes($val);</span><br><span class="line">                $res[$key] = $val;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">unset</span>($data);</span><br><span class="line">        <span class="keyword">return</span> $res;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Table info</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_tbInfo</span><span class="params">(string $table = <span class="string">''</span>)</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $table = !<span class="keyword">empty</span>($table) ? $table : <span class="keyword">self</span>::$table;</span><br><span class="line">        $sql = <span class="string">'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME="'</span>.$table.<span class="string">'" AND TABLE_SCHEMA="'</span>.<span class="keyword">self</span>::$db.<span class="string">'"'</span>;</span><br><span class="line">        !<span class="keyword">empty</span>(<span class="keyword">self</span>::$conn) <span class="keyword">or</span> <span class="keyword">self</span>::_connect();</span><br><span class="line">        $statm = <span class="keyword">self</span>::$conn-&gt;prepare($sql);</span><br><span class="line">        $statm-&gt;execute();</span><br><span class="line">        $result = $statm-&gt;fetchAll(\PDO::FETCH_ASSOC);</span><br><span class="line">        $res = [];</span><br><span class="line">        <span class="keyword">foreach</span> ($result <span class="keyword">as</span> $key=&gt;$value) &#123;</span><br><span class="line">            $res[$value[<span class="string">'COLUMN_NAME'</span>]] = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// unset($result, $statm);</span></span><br><span class="line">        <span class="keyword">return</span> $res;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Get primary key</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_tbKey</span><span class="params">(string $table)</span>:<span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $sql = <span class="string">'SELECT k.column_name FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING (constraint_name,table_schema,table_name) WHERE t.constraint_type="PRIMARY KEY" AND t.table_schema="'</span>.<span class="keyword">self</span>::$db.<span class="string">'" AND t.table_name="'</span>.$table.<span class="string">'"'</span>;</span><br><span class="line">        !<span class="keyword">empty</span>(<span class="keyword">self</span>::$conn) <span class="keyword">or</span> <span class="keyword">self</span>::_connect();</span><br><span class="line">        $statm = <span class="keyword">self</span>::$conn-&gt;prepare($sql);</span><br><span class="line">        $statm-&gt;execute();</span><br><span class="line">        $result = $statm-&gt;fetchAll(\PDO::FETCH_ASSOC);</span><br><span class="line">        $res = [];</span><br><span class="line">        <span class="keyword">foreach</span> ($result <span class="keyword">as</span> $key=&gt;$value) &#123;</span><br><span class="line">            $res[$value[<span class="string">'column_name'</span>]] = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">unset</span>($result, $statm);</span><br><span class="line">        <span class="keyword">return</span> $res;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Avoid mistakes</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">_avoidKey</span><span class="params">(string $value)</span>:<span class="title">string</span></span></span><br><span class="line"><span class="function">    </span>&#123; </span><br><span class="line">        <span class="keyword">if</span> (<span class="string">'*'</span> == $value || <span class="keyword">false</span> !== strpos($value,<span class="string">'('</span>) || <span class="keyword">false</span> !== strpos($value,<span class="string">'.'</span>));</span><br><span class="line">        <span class="keyword">elseif</span> (<span class="keyword">false</span> === strpos($value,<span class="string">'`'</span>)) $value = <span class="string">'`'</span>.trim($value).<span class="string">'`'</span>;</span><br><span class="line">        <span class="keyword">return</span> $value; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h3><p>兼容两种写法,自动绑定与参数形式,参数形式写法优先于自动绑定</p>
<h4 id="自动绑定写法"><a href="#自动绑定写法" class="headerlink" title="自动绑定写法:"></a>自动绑定写法:</h4><p>Example-1:初始化<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// step_1:引入文件</span></span><br><span class="line"><span class="keyword">require_once</span> <span class="string">'mysql.php'</span>;</span><br><span class="line"><span class="comment">// 取别名(可省略)</span></span><br><span class="line"><span class="keyword">use</span> <span class="title">mysql_pdo</span> <span class="title">as</span> <span class="title">db</span>;</span><br><span class="line"><span class="comment">// 连接信息(可省略,可在文件内写好配置)</span></span><br><span class="line">db::$host = <span class="string">'127.0.0.1'</span>;</span><br><span class="line">db::$user = <span class="string">'root'</span>;</span><br><span class="line">db::$pwd = <span class="string">'root'</span>;</span><br><span class="line">db::$db = <span class="string">'test'</span>;</span><br><span class="line"><span class="comment">// step_2:绑定操作信息</span></span><br><span class="line">db::$tbale = <span class="string">'tablename'</span>;</span><br><span class="line"><span class="comment">// step_3:开始使用</span></span><br><span class="line">db::select();  <span class="comment">// 该操作最终执行的语句是:SELECT * FROM `tablename`</span></span><br></pre></td></tr></table></figure></p>
<p>Example-2:Insert操作<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 绑定操作表</span></span><br><span class="line">db::$table = <span class="string">'tablename'</span>;</span><br><span class="line"><span class="comment">// 绑定数据,如需插入多行数据,构建的数据为二维数组即可</span></span><br><span class="line">db::$data = [</span><br><span class="line">    <span class="string">'col_1'</span> =&gt; <span class="string">'value_1'</span>,</span><br><span class="line">    <span class="string">'col_2'</span> =&gt; <span class="string">'value_2'</span>,</span><br><span class="line">    <span class="string">'col_3'</span> =&gt; <span class="string">'value_3'</span>,</span><br><span class="line">    <span class="string">'col_4'</span> =&gt; <span class="string">'value_4'</span></span><br><span class="line">];</span><br><span class="line"><span class="comment">// 执行</span></span><br><span class="line">db::insert();  </span><br><span class="line"><span class="comment">// 返回值为最后插入的ID,如果要获取插入的行数请使用传参写法 db::insert('tablename',$data,true) 参数形式无须绑定参数</span></span><br></pre></td></tr></table></figure></p>
<p>Example-3:Delete操作<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 操作表,如果每次都对同一张表进行操作,则不需要重新绑定</span></span><br><span class="line">db::$table = <span class="string">'tablename'</span>;</span><br><span class="line"><span class="comment">// 绑定where条件,防止删表操作,在没有where条件下不会执行delete操作</span></span><br><span class="line">db::$where = [<span class="string">'id'</span> =&gt; <span class="number">1</span>] <span class="comment">// 数组形式一 最终解析:[AND] `id` = 1 默认对比关系为 =,逻辑 AND</span></span><br><span class="line">db::$where = [<span class="string">'id'</span> =&gt; [<span class="string">'1'</span>,<span class="string">'&gt;'</span>,<span class="string">'AND'</span>]] <span class="comment">// 数组形式二 最终解析:[AND] `id` &gt; 1</span></span><br><span class="line">db::$where = <span class="string">'id = 1'</span>  <span class="comment">//字符串形式 最终解析为: WHERE id = 1</span></span><br><span class="line"><span class="comment">// 执行</span></span><br><span class="line">db::del();</span><br></pre></td></tr></table></figure></p>
<p>Example-4:Update操作<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">// 在绑定表的基础上绑定数据即可</span></span><br><span class="line">db::$data = [</span><br><span class="line">    <span class="string">'col_1'</span> =&gt; <span class="string">'value_1'</span>,</span><br><span class="line">    <span class="string">'col_2'</span> =&gt; <span class="string">'update_2'</span>,</span><br><span class="line">    <span class="string">'col_3'</span> =&gt; <span class="string">'update_3'</span>,</span><br><span class="line">    <span class="string">'col_4'</span> =&gt; <span class="string">'update_4'</span></span><br><span class="line">];</span><br><span class="line"><span class="comment">// 当上述上中col_1为主键时,value_1,将自动当WHERE条件,解析为: WHERE `col_1` = value_1,至少要存在一个主键</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 更新</span></span><br><span class="line">db::save();</span><br><span class="line"><span class="comment">// 如果绑定$where条件,则上述主键效果将消失</span></span><br><span class="line">db::$where = [opt...];  <span class="comment">// 值的写法同上</span></span><br></pre></td></tr></table></figure></p>
<p>Example-5:Select操作<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// join表,绑定操作表与where绑定不再演示</span></span><br><span class="line">db::$join = [<span class="string">'table_b'</span>=&gt;[<span class="string">'table_a.id'</span>,<span class="string">'table_b.aid'</span>,<span class="string">'INNER'</span>,<span class="string">'='</span>]];</span><br><span class="line"><span class="comment">// 一维数组中的键名为连接表的表名,二维数组中前两个值为连接条件-必填项,第三个默认为INNER,第四个默认为=</span></span><br><span class="line"></span><br><span class="line">db::$join = [<span class="string">'table_b'</span>=&gt;<span class="string">'LEFT JOIN table_b ON table_a.id=table_b.aid'</span>];</span><br><span class="line"><span class="comment">//当值为一个字符串时,将直接拼接</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 分组查询 </span></span><br><span class="line">db::$group = [<span class="string">'sex'</span>,<span class="string">'age'</span>];  <span class="comment">// 值为要分组的列名字即可</span></span><br><span class="line">db::$group = <span class="string">'sex'</span>  		 <span class="comment">// 值为字符串时直接拼接为 GROUP BY sex</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 排序</span></span><br><span class="line">db::$order = [<span class="string">'id'</span> =&gt; <span class="string">'DESC'</span>];  <span class="comment">// 关联数组将解析成 ORDER BY `id` DESC</span></span><br><span class="line">db::$order = [<span class="string">'id'</span>];  			<span class="comment">// 索引数组将解析成 ORDER BY `id` ASC,默认为ASC排序</span></span><br><span class="line">db::$order = <span class="string">'id'</span>;  			<span class="comment">// 字符串将解析成 ORDER BY id 自动拼接</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 分页</span></span><br><span class="line">db::$limit = [<span class="number">0</span>,<span class="number">10</span>]  <span class="comment">// 存在两个值时将解析为 LIMIT 0,10;</span></span><br><span class="line">db::$limit = [<span class="number">1</span>]     <span class="comment">// 存在一个值时将解析为 LIMIT 1;</span></span><br><span class="line">db::$limit = <span class="string">'0,20'</span>  <span class="comment">// 字符串将解析为 LIMIT 0,20;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 查询</span></span><br><span class="line">db::select();  <span class="comment">// 多条</span></span><br><span class="line">db::first();   <span class="comment">// 第一条</span></span><br></pre></td></tr></table></figure></p>
<p>Example-6:聚合函数<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">db::count(); <span class="comment">// count</span></span><br><span class="line"></span><br><span class="line">db::sum();   <span class="comment">// sum</span></span><br><span class="line"></span><br><span class="line">db::avg();   <span class="comment">// avg</span></span><br><span class="line"></span><br><span class="line">db::min();   <span class="comment">// min</span></span><br><span class="line"></span><br><span class="line">db::max();   <span class="comment">// max</span></span><br></pre></td></tr></table></figure></p>
<p>Example-7:附加1<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">db::begin(); 			 <span class="comment">// 开启事务</span></span><br><span class="line"></span><br><span class="line">db::commit(); 			 <span class="comment">// 提交事务</span></span><br><span class="line"></span><br><span class="line">db::rollBack(); 		 <span class="comment">// 回滚事务</span></span><br><span class="line"></span><br><span class="line">db::setAttr($key, $val); <span class="comment">// 设置PDO属性</span></span><br><span class="line"></span><br><span class="line">db::dec(); 				 <span class="comment">// 字段自增,需绑定数据 db::$data = ['age'=&gt;2]该自增步长为2,省略则默认为1</span></span><br><span class="line"></span><br><span class="line">db::inc(); 				 <span class="comment">// 字段自增,需绑定数据 db::$data = ['age'=&gt;2]该自减步长为2,省略则默认为1</span></span><br><span class="line"></span><br><span class="line">db::do($sql, [$flag]); 	 <span class="comment">//执行一条SQL语句,不分查询还是执行,自动分配.第二个参数同下</span></span><br><span class="line"></span><br><span class="line">db::query($sql,[$flag]); <span class="comment">//执行一条有结果集的SQL语句, $flag 默认为false,为true是返回一条记录</span></span><br><span class="line"></span><br><span class="line">db::exec($sql, [$flag]); <span class="comment">//执行一条无结果集的SQL语句, $flag 默认为false,为true时返回insert语句的插入的ID,其他语句无效</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 执行拼接SQL可使用预处理绑定参数形式,然后绑定$bind即可</span></span><br><span class="line">$sql = <span class="string">'UPDATE `tablename` SET col_a=:col_a WHERE id=:id'</span>;</span><br><span class="line">db::$bind = [<span class="string">':col_a'</span> =&gt; <span class="string">'update_a'</span>,<span class="string">':id'</span>=&gt;<span class="number">1</span>];</span><br><span class="line">db::do($sql); <span class="comment">//或者 db::exec($sql);</span></span><br><span class="line"></span><br><span class="line">db::clear() <span class="comment">// 该方法将重置$data,$field,$where,$order,$group,$limit,$join,$bind数据,默认每执行一次语句将自动重置</span></span><br><span class="line"></span><br><span class="line">db::init([conf...], $reconnect = <span class="keyword">false</span>); <span class="comment">// conf数组的值可为数据库基本配置(host,user,pwd等) $reconnext为true是将重新连接mysql</span></span><br></pre></td></tr></table></figure></p>
<h4 id="传参写法"><a href="#传参写法" class="headerlink" title="传参写法"></a>传参写法</h4><p>Example-8:附加2<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// insert</span></span><br><span class="line">db::inster(<span class="string">'tablename'</span>,[data...],$where);</span><br><span class="line"></span><br><span class="line"><span class="comment">// delete</span></span><br><span class="line">db::del(<span class="string">'tablename'</span>, $where);</span><br><span class="line"></span><br><span class="line"><span class="comment">// update</span></span><br><span class="line">db::save(<span class="string">'tablename'</span>, [data...], $where);</span><br><span class="line"></span><br><span class="line"><span class="comment">// select</span></span><br><span class="line">db::select(<span class="string">'tablename'</span>,[opt..]);  <span class="comment">//opt参数可传入field,where,join,order,group,limit等信息</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// first</span></span><br><span class="line">db::first(<span class="string">'tablename'</span>,[opt..]);   <span class="comment">//opt参数同上</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// avg</span></span><br><span class="line">db::avg(<span class="string">'tablename'</span>,[opt..]);  	  <span class="comment">//opt参数同上</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// count</span></span><br><span class="line">db::count(<span class="string">'tablename'</span>,[opt..]);   <span class="comment">//opt参数同上</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// sum</span></span><br><span class="line">db::sum(<span class="string">'tablename'</span>,[opt..]);     <span class="comment">//opt参数同上</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// min</span></span><br><span class="line">db::min(<span class="string">'tablename'</span>,[opt..]);     <span class="comment">//opt参数同上</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// max</span></span><br><span class="line">db::max(<span class="string">'tablename'</span>,[opt..]);     <span class="comment">//opt参数同上</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// dec</span></span><br><span class="line">db::dec(<span class="string">'tablename'</span>,[data..],$where);    <span class="comment">//参数说明同绑定参数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// inc</span></span><br><span class="line">db::inc(<span class="string">'tablename'</span>,[data..],$where);    <span class="comment">//参数说明同绑定参数</span></span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>本文出自个人博客 <strong><a href="https://blog.wm07.cn/2017/pdo_func.html">最好的安排</a></strong> 转载请注明出处!</p>
</blockquote>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/mysql/" rel="tag"># mysql</a>
          
            <a href="/tags/php/" rel="tag"># php</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/docker-nginx-slb.html" rel="next" title="docker搭建nginx服务器集群">
                <i class="fa fa-chevron-left"></i> docker搭建nginx服务器集群
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/hero.html" rel="prev" title="使用PHP开发一个直播答题助手">
                使用PHP开发一个直播答题助手 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://cdn.wm07.cn/pic.png"
                alt="空城" />
            
              <p class="site-author-name" itemprop="name">空城</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/phpxiaowei" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://segmentfault.com/u/jiachengxuyuan_59c0accf5a1ed" target="_blank" title="SegmentFault">
                    
                      <i class="fa fa-fw fa-meh-o"></i>SegmentFault</a>
                </span>
              
            
          </div>

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-globe"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://tianshengbufan.com" title="天生不凡博客" target="_blank" rel="external nofollow">天生不凡博客</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#先贴代码"><span class="nav-number">1.</span> <span class="nav-text">先贴代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基本使用"><span class="nav-number">2.</span> <span class="nav-text">基本使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#自动绑定写法"><span class="nav-number">2.1.</span> <span class="nav-text">自动绑定写法:</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#传参写法"><span class="nav-number">2.2.</span> <span class="nav-text">传参写法</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">空城</span>

  
  <span class="post-meta-divider">|</span><a class="theme-link" target="_blank" href="http://www.miitbeian.gov.cn/" rel="external nofollow">湘ICP备17007213号</a>
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io" rel="external nofollow">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>




  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">NexT.Gemini</a> v5.1.3</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'oDLKa1IlCi8XzHWzfdD4OCGS-gzGzoHsz',
        appKey: 'ggsl72lxBKGDysOTdpd06hBA',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("oDLKa1IlCi8XzHWzfdD4OCGS-gzGzoHsz", "ggsl72lxBKGDysOTdpd06hBA");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  

  

  

</body>
</html>
